using System;
using System.Configuration;
using System.IO;
using Drill.TestSupport;
using NUnit.Framework;

namespace Drill.Core.Utilities
{
    [TestFixture]
    public class ConfigurationUtilityTests
    {
        [Category("IntegrationTest")]
        [Test]
        public void CanLoadConfigFile()
        {
            string absolutePath = TestConfigFileLoader.GetDefaultTestConfigurationFilename();

            System.Configuration.Configuration config = ConfigurationUtility.LoadConfigFile(absolutePath);

            Assert.That(config, Is.Not.Null);
        }

        [Category("UnitTest")]
        [ExpectedException(typeof(ArgumentException), ExpectedMessage = "cannot be empty", MatchType = MessageMatch.Contains)]
        [Test]
        public void CannotLoadConfigFileWithEmptyFilenameParameter()
        {
            ConfigurationUtility.LoadConfigFile("");
        }

        [Category("UnitTest")]
        [ExpectedException(typeof(ArgumentNullException), ExpectedMessage = "cannot be null", MatchType = MessageMatch.Contains)]
        [Test]
        public void CannotLoadConfigFileWithNullFilenameParameter()
        {
            ConfigurationUtility.LoadConfigFile(null);
        }

        [Category("UnitTest")]
        [ExpectedException(typeof(ArgumentException), ExpectedMessage = "cannot contain only whitespace characters", MatchType = MessageMatch.Contains)]
        [Test]
        public void CannotLoadConfigFileWithFilenameParameterContainingOnlyWhitespaceCharacters()
        {
            ConfigurationUtility.LoadConfigFile(" ");
        }

        [Category("UnitTest")]
        [ExpectedException(typeof(FileNotFoundException), ExpectedMessage = "cannot be loaded because the file could not be found", MatchType = MessageMatch.Contains)]
        [Test]
        public void CannotLoadConfigFileWhenFileCannotBeFound()
        {
            ConfigurationUtility.LoadConfigFile(@"C:\Fake\Path\Qwerty\Asdf\Zxcvbnm\FakeConfig.fakeConfigFile.ext");
        }

        [Category("IntegrationTest")]
        [Test]
        public void CanGetConfigSectionFromConfigFile()
        {
			const string configSectionName = "drill/dependencyResolvers";

            string absolutePath = TestConfigFileLoader.GetDefaultTestConfigurationFilename();

            ConfigurationSection configSection = ConfigurationUtility.GetConfigSectionFromConfigFile(configSectionName, absolutePath);

            Assert.That(configSection, Is.Not.Null);
        }

        [Category("UnitTest")]
        [ExpectedException(typeof(ArgumentException), ExpectedMessage = "cannot be empty", MatchType = MessageMatch.Contains)]
        [Test]
        public void CannotGetConfigSectionFromConfigFileWithEmptyFilenameParameter()
        {
            ConfigurationUtility.GetConfigSectionFromConfigFile("testConfigSectionName", "");
        }

        [Category("UnitTest")]
        [ExpectedException(typeof(ArgumentNullException), ExpectedMessage = "cannot be null", MatchType = MessageMatch.Contains)]
        [Test]
        public void CannotGetConfigSectionFromConfigFileWithNullFilenameParameter()
        {
            ConfigurationUtility.GetConfigSectionFromConfigFile("testConfigSectionName", null);
        }

        [Category("UnitTest")]
        [ExpectedException(typeof(ArgumentException), ExpectedMessage = "cannot contain only whitespace characters", MatchType = MessageMatch.Contains)]
        [Test]
        public void CannotGetConfigSectionFromConfigFileWithFilenameParameterContainingOnlyWhitespaceCharacters()
        {
            ConfigurationUtility.GetConfigSectionFromConfigFile("testConfigSectionName", " ");
        }

        [Category("UnitTest")]
        [ExpectedException(typeof(FileNotFoundException), ExpectedMessage = "cannot be loaded because the file could not be found", MatchType = MessageMatch.Contains)]
        [Test]
        public void CannotGetConfigSectionFromConfigFileWhenFileCannotBeFound()
        {
            ConfigurationUtility.GetConfigSectionFromConfigFile("testConfigSectionName", @"C:\Fake\Path\Qwerty\Asdf\Zxcvbnm\FakeConfig.fakeConfigFile.ext");
        }

        [Category("UnitTest")]
        [ExpectedException(typeof(ArgumentException), ExpectedMessage = "cannot be empty", MatchType = MessageMatch.Contains)]
        [Test]
        public void CannotGetConfigSectionFromConfigFileWithEmptyConfigSectionNameParameter()
        {
            ConfigurationUtility.GetConfigSectionFromConfigFile("", @"C:\Fake\Path\Qwerty\Asdf\Zxcvbnm\FakeConfig.fakeConfigFile.ext");
        }

        [Category("UnitTest")]
        [ExpectedException(typeof(ArgumentNullException), ExpectedMessage = "cannot be null", MatchType = MessageMatch.Contains)]
        [Test]
        public void CannotGetConfigSectionFromConfigFileWithNullConfigSectionNameParameter()
        {
            ConfigurationUtility.GetConfigSectionFromConfigFile(null, @"C:\Fake\Path\Qwerty\Asdf\Zxcvbnm\FakeConfig.fakeConfigFile.ext");
        }

        [Category("UnitTest")]
        [ExpectedException(typeof(ArgumentException), ExpectedMessage = "cannot contain only whitespace characters", MatchType = MessageMatch.Contains)]
        [Test]
        public void CannotGetConfigSectionFromConfigFileWithConfigSectionNameParameterContainingOnlyWhitespaceCharacters()
        {
            ConfigurationUtility.GetConfigSectionFromConfigFile(" ", @"C:\Fake\Path\Qwerty\Asdf\Zxcvbnm\FakeConfig.fakeConfigFile.ext");
        }

        [Category("IntegrationTest")]
        [Test]
        public void CannotGetConfigSectionFromDefaultConfigFileWithInvalidConfigSectionNameParameter()
        {
            ConfigurationSection configSection = ConfigurationUtility.GetConfigSectionFromConfigFile("FakeConfigSectionNameUsedForTesting");

            Assert.That(configSection, Is.Null);
        }

        [Category("UnitTest")]
        [ExpectedException(typeof(ArgumentException), ExpectedMessage = "cannot be empty", MatchType = MessageMatch.Contains)]
        [Test]
        public void CannotGetConfigSectionFromDefaultConfigFileWithEmptyConfigSectionNameParameter()
        {
            ConfigurationUtility.GetConfigSectionFromConfigFile("");
        }

        [Category("UnitTest")]
        [ExpectedException(typeof(ArgumentNullException), ExpectedMessage = "cannot be null", MatchType = MessageMatch.Contains)]
        [Test]
        public void CannotGetConfigSectionFromDefaultConfigFileWithNullConfigSectionNameParameter()
        {
            ConfigurationUtility.GetConfigSectionFromConfigFile(null);
        }

        [Category("UnitTest")]
        [ExpectedException(typeof(ArgumentException), ExpectedMessage = "cannot contain only whitespace characters", MatchType = MessageMatch.Contains)]
        [Test]
        public void CannotGetConfigSectionFromDefaultConfigFileWithConfigSectionNameParameterContainingOnlyWhitespaceCharacters()
        {
            ConfigurationUtility.GetConfigSectionFromConfigFile(" ");
        }
    }
}